package com.dtx.controller;

import com.dtx.domain.Exam;
import com.dtx.service.IExamService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;

    //显示全部试题
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
                                @RequestParam(name = "size", required = true, defaultValue = "4") Integer size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Exam> examList = examService.findAll(page, size);
        //PageInfo就是一个分页Bean
        PageInfo pageInfo = new PageInfo(examList);
        mv.addObject("examList", examList);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("exam-list");
        return mv;
    }

    //按照id寻找题目是试题修改的基础
    @RequestMapping("/findById.do")
    public ModelAndView findById(@RequestParam(name = "id", required = true) Integer id) throws Exception {
        ModelAndView mv = new ModelAndView();
        Exam exam = examService.findById(id);
        mv.addObject("exam", exam);
        mv.setViewName("exam-modify");
        return mv;
    }

    //试题添加
    @RequestMapping("/save.do")
    public String save(Exam exam) throws Exception {
        examService.save(exam);
        return "redirect:findAll.do";
    }

    //删除试题
    @RequestMapping("/deleteExam")
    public String deleteExam(Integer id) throws Exception {
        examService.deleteExamById(id);
        return "redirect:findAll.do";
    }

    //试题修改
    @RequestMapping("/update.do")
    public String updateById(Exam exam) throws Exception {
        examService.update(exam);
        return "redirect:findAll.do";
    }


    // 查询试题
    @RequestMapping("/findExamByName.do")
    public ModelAndView  findExamByName(String name) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Exam> examList = examService.findExamByName(name);
        mv.addObject("examList", examList);
        mv.setViewName("exam-list");
        return mv;
    }
}
